package com.stone.framework.basic.param.controller;

import java.util.stream.Stream;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.stone.framework.basic.param.service.BusinessParamService;
import com.stone.framework.message.PageReqMessage;
import com.stone.framework.message.PageResMessage;
import com.stone.framework.message.ReqMessage;
import com.stone.framework.message.ResMessage;
import com.stone.framework.message.basic.param.BusinessParamPageReqBody;
import com.stone.framework.message.basic.param.BusinessParamPageResBody;
import com.stone.framework.message.basic.param.BusinessParamReqBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "业务参数")
@RestController
@RequestMapping("/param/business")
public class BusinessParamController {

    @Autowired
    private BusinessParamService businessParamService;

    @ApiOperation(value = "分页查询", httpMethod = "POST")
    @PostMapping("/getPage")
    public PageResMessage<BusinessParamPageResBody>
        getPage(@Valid @RequestBody PageReqMessage<BusinessParamPageReqBody> reqMessage) {
        Page<BusinessParamPageResBody> page =
            businessParamService.queryPage(reqMessage.getBody(), reqMessage.buildPageable());
        return PageResMessage.success(page);
    }

    @ApiOperation(value = "查询", httpMethod = "POST")
    @PostMapping("/getValue")
    public ResMessage<String> getValue(@Valid @RequestBody ReqMessage<String> reqMessage) {
        String value = businessParamService.queryValue(reqMessage.getBody());
        return PageResMessage.success(value);
    }

    @ApiOperation(value = "新增", httpMethod = "POST")
    @PostMapping(value = "/add")
    ResMessage<Void> add(@Valid @RequestBody ReqMessage<BusinessParamReqBody> reqMessage) {
        businessParamService.save(reqMessage.getBody());
        return ResMessage.success();
    }

    @ApiOperation(value = "修改", httpMethod = "POST")
    @PostMapping(value = "/update")
    ResMessage<Void> update(@Valid @RequestBody ReqMessage<BusinessParamReqBody> reqMessage) {
        businessParamService.modify(reqMessage.getBody());
        return ResMessage.success();
    }

    @ApiOperation(value = "刪除", httpMethod = "POST")
    @PostMapping(value = "/delete")
    ResMessage<Void> delete(@Valid @RequestBody ReqMessage<BusinessParamReqBody> reqMessage) {
        businessParamService.delete(reqMessage.getBody());
        return ResMessage.success();
    }

    @ApiOperation(value = "刷新缓存，支持多选", httpMethod = "POST")
    @PostMapping(value = "/refreshCache")
    ResMessage<Void> refreshCache(@Valid @RequestBody ReqMessage<String> reqMessage) {
        if (reqMessage.getBody() == null) {
            businessParamService.refreshCache();
        } else {
            String[] keys = reqMessage.getBody().split(",");
            Stream.of(keys).forEach(key -> businessParamService.refreshCache(key));
        }
        return ResMessage.success();
    }

}
